package ueg.org.trabalho.view;

import java.awt.Dimension;
import javax.swing.JOptionPane;
import ueg.org.trabalho.dao.DaoDepartamento;
import ueg.org.trabalho.dao.DaoFuncionario;
import ueg.org.trabalho.interfaces.IDao;
import ueg.org.trabalho.model.Departamento;
import ueg.org.trabalho.model.Funcionario;
import ueg.org.trabalho.util.Util;

public class GUICadastroFuncionario extends javax.swing.JInternalFrame {

    public GUICadastroFuncionario() {
        initComponents();
        getDepartamento();

    }

    public void setPosicao() {
        Dimension d = this.getDesktopPane().getSize();
        this.setLocation((d.width - this.getSize().width) / 2,
                (d.height - this.getSize().height) / 2);
    }

    public void getDepartamento() {
        IDao<Departamento> iDao = new DaoDepartamento();
        try {
            for (Departamento d : iDao.listagem()) {
                cbDepartamento.addItem(d.getNome());
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Erro ao Listar Empresas: " + ex);
        }
    }

    public Funcionario setFuncionario() {
        Funcionario funcionario = new Funcionario();
        funcionario.setNome(tfNome.getText().trim());
        funcionario.setMatricula(tfMatricula.getText().trim());

        return funcionario;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        CadastroFuncionario = new javax.swing.JPanel();
        lbNome = new javax.swing.JLabel();
        tfNome = new javax.swing.JTextField();
        lbDepartamento = new javax.swing.JLabel();
        cbDepartamento = new javax.swing.JComboBox();
        lbMatricula = new javax.swing.JLabel();
        tfMatricula = new javax.swing.JTextField();
        bSalvar = new javax.swing.JButton();
        bCancelar = new javax.swing.JButton();
        bLimparCampos = new javax.swing.JButton();

        setClosable(true);

        CadastroFuncionario.setBorder(javax.swing.BorderFactory.createTitledBorder("Cadastro Funcionario"));

        lbNome.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lbNome.setText("Nome");

        lbDepartamento.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lbDepartamento.setText("Departamento");

        cbDepartamento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbDepartamentoActionPerformed(evt);
            }
        });

        lbMatricula.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lbMatricula.setText("Matricula");

        javax.swing.GroupLayout CadastroFuncionarioLayout = new javax.swing.GroupLayout(CadastroFuncionario);
        CadastroFuncionario.setLayout(CadastroFuncionarioLayout);
        CadastroFuncionarioLayout.setHorizontalGroup(
            CadastroFuncionarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(CadastroFuncionarioLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(CadastroFuncionarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(CadastroFuncionarioLayout.createSequentialGroup()
                        .addComponent(lbNome)
                        .addGap(28, 28, 28)
                        .addComponent(tfNome, javax.swing.GroupLayout.PREFERRED_SIZE, 352, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(CadastroFuncionarioLayout.createSequentialGroup()
                        .addComponent(lbDepartamento)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cbDepartamento, javax.swing.GroupLayout.PREFERRED_SIZE, 317, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(CadastroFuncionarioLayout.createSequentialGroup()
                        .addComponent(lbMatricula)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(tfMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, 352, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(76, Short.MAX_VALUE))
        );
        CadastroFuncionarioLayout.setVerticalGroup(
            CadastroFuncionarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(CadastroFuncionarioLayout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(CadastroFuncionarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbNome)
                    .addComponent(tfNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(CadastroFuncionarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbMatricula)
                    .addComponent(tfMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                .addGroup(CadastroFuncionarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbDepartamento)
                    .addComponent(cbDepartamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(45, 45, 45))
        );

        bSalvar.setText("Salvar");
        bSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bSalvarActionPerformed(evt);
            }
        });

        bCancelar.setText("Cancelar");
        bCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCancelarActionPerformed(evt);
            }
        });

        bLimparCampos.setText("Limpar Campos");
        bLimparCampos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bLimparCamposActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(CadastroFuncionario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(82, 82, 82)
                        .addComponent(bSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(30, 30, 30)
                        .addComponent(bCancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(bLimparCampos)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {bCancelar, bSalvar});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(CadastroFuncionario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 57, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bCancelar)
                    .addComponent(bSalvar)
                    .addComponent(bLimparCampos))
                .addGap(26, 26, 26))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {bCancelar, bSalvar});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cbDepartamentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbDepartamentoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbDepartamentoActionPerformed

    private void bCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_bCancelarActionPerformed

    private void bLimparCamposActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bLimparCamposActionPerformed
        try {
            Util.LimpaTela(CadastroFuncionario);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Não foi possivel limpar os campos!");
        }
    }//GEN-LAST:event_bLimparCamposActionPerformed

    private void bSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bSalvarActionPerformed
        Funcionario funcionario = new Funcionario();
        funcionario = setFuncionario();
        IDao<Funcionario> iDao = new DaoFuncionario();
        try {
            iDao.salva(funcionario);
            JOptionPane.showMessageDialog(null, "Salvo com sucesso!");
            Util.LimpaTela(CadastroFuncionario);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Não foi possivel salvar o funcionario!motivo: " + e);
        }
    }//GEN-LAST:event_bSalvarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel CadastroFuncionario;
    private javax.swing.JButton bCancelar;
    private javax.swing.JButton bLimparCampos;
    private javax.swing.JButton bSalvar;
    private javax.swing.JComboBox cbDepartamento;
    private javax.swing.JLabel lbDepartamento;
    private javax.swing.JLabel lbMatricula;
    private javax.swing.JLabel lbNome;
    private javax.swing.JTextField tfMatricula;
    private javax.swing.JTextField tfNome;
    // End of variables declaration//GEN-END:variables
}
